<?
include("../lib/config.php");
include("../header.php");
include("../lib/function.php");
include("../lib/header.php");
//include("header.php");
$show	= $HTTP_GET_VARS['show'];
$page 	= $HTTP_GET_VARS['page']; // get nomor halaman yang dipanggil
$next	= $HTTP_GET_VARS['next']; // get segmen halaman yang dipanggil
$tahun	= $HTTP_GET_VARS['tahun'];
$level	= $HTTP_GET_VARS['level'];
$keyword= $HTTP_GET_VARS['keyword'];

?>
<script type="text/javascript">
	function hapus(USR)
	{
		var ok = confirm("Apakah Anda yakin menghapus user "+ USR +"  ?");
		if(ok == true)
		{
			window.location.href="../lib/delete.php?cat=add_user&usr="+USR+"";
		}
		else
		{
		}
	}
</script>
<?
	// khusus untuk administrasi user
	$sads = mssql_query("SELECT * FROM VPrivUser WHERE username='".$_SESSION['user_login']."' AND nama_menu='administrasi_user'",$conn);
	$rads = mssql_fetch_array($sads);
?>
</head>
<table width="93%" class="table_indent2" class="no_border">
 <tr>
  <td colspan="2">
	<table style="border:none;">
	<tr>
	 <td colspan="2" class="line_bottom">&nbsp;&nbsp;<b>TABEL USER</b></td>
	</tr>
	<tr>
	</table>
  </td>
 </tr>
 <tr>
  <td align="center">
  <form action="<?=$PHP_SELF?>" method="get">
   Cari Username: <input type="text" name="keyword" /> <input type="submit" value="Cari" class="button" />
  </form>
  </td>
 </tr>
 <tr>
  <td>
  <?
  
  if(!empty($keyword)){
  		// untuk pencarian berdasarkan nama pegawai
		$nnn  = mssql_query("SELECT NIK, Nama FROM Karyawan WHERE Nama LIKE '%$keyword%'",$conn);
		//echo "SELECT NIK, Nama FROM Karyawan WHERE Nama LIKE '%$keyword%'";
		if(mssql_num_rows($nnn) > 0){
			$rnn  = mssql_fetch_array($nnn);
  			$cari = " AND username LIKE '%".$rnn[NIK]."%' ";
			//echo"$rnn[NIK]";
			//exit;
		}
		else
		{
  			$cari = " AND username LIKE '%$keyword%' ";
		}
  }
  else{
  		$cari = "";
  }
  
  	$result	= mssql_query("SELECT ROW_NUMBER() OVER (ORDER BY username) As No, * FROM S_User A, S_User_Group B WHERE A.groupid=B.group_id $cari ORDER BY username ASC",$conn);
	$total	= mssql_num_rows($result);
	$limit	= 20;
	$pager  = Pager::getPagerData($total, $limit, $page);
	$offset = $pager->offset;
	$limit  = $pager->limit;
	$page   = $pager->page;
	Paging1($pager,$page,$next,$limit,$keyword,$show);
  ?>
     <table cellpadding="4" cellspacing="1" width="93%">
      <tr>
       <td colspan="2">
       <fieldset><legend>Daftar User</legend>
		<table width="100%">
         <tr class="headlist">
          <td width="10">No.</td>
          <td width="100">Username</td>
          <td>Nama Lengkap</td>
          <td>Group User</td>
		  <td colspan="3">&nbsp;</td>
         </tr>
         <?
         	$sw = mssql_query("WITH limit AS (SELECT ROW_NUMBER() OVER (ORDER BY username) As No, * FROM S_User A, S_User_Group B WHERE A.groupid=B.group_id $cari ) SELECT * FROM limit WHERE No BETWEEN ".$offset." AND ".($offset+$limit)."",$conn);
			while($rw = mssql_fetch_array($sw)){
				echo"
				 <tr class=contentlist>
				  <td>$rw[No]</td>
				  <td>$rw[username]</td>
				  <td>
				  ";
				$unik	= mssql_query("SELECT nama FROM Karyawan WHERE NIK='$rw[username]'",$conn);  
				if($unik){
					$runik = mssql_fetch_array($unik);
					echo"$runik[0]";
				}
				else{
				
				}
				echo"
				  </td>
				  <td>$rw[group_name]</td>";
				// hak akses reset password
				$sadspwd = mssql_query("SELECT * FROM VPrivUser WHERE username='".$_SESSION['user_login']."' AND nama_menu='adm_reset_password'",$conn);
				$radspwd = mssql_fetch_array($sadspwd);
				if($radspwd[edit] == 1){
					echo"<td width=110>[ <a href='frm_adm_reset_pass.php?u=$rw[username]'>reset password</a> ]</td>";
				}
          		// hak akses edit
				if($rads[edit] == 1){
					echo"
				  <td width=1%><a href='frm_adm_add_user.php?u=$rw[username]&amp;op=edit'><img src='../images/b_edit.png' border='0'></a></td>";
				}
          		// hak akses hapus
				if($rads[hapus] == 1){
				  echo"
				  <td width=1%><a href='#' onclick=\"hapus('$rw[username]');\"><img src='../images/b_delete.png' border='0'></a></td>";
				}
				echo"
				 </tr>
				";
			}
		 ?>
         <tr>
          <td colspan="3" align="right"><br />
          <?
          	if($rads[tambah] == 1){
		  ?>
          <input type="button" value="Tambah User" class="button" onclick="Javascript:location.href='frm_adm_add_user.php'" />
          <?
          }
		  ?>
          </td>
         </tr>
        </table>
       </fieldset>
       </td>
      </tr>
     </table>
  </td>
</tr>
</table>
<?
echo"</div>
	  </td>
	   </tr>
	    </table>
";
include("../menu.php");
?>